package com.hjl.dao.impl;

import com.hjl.dao.JobDao;
import com.hjl.domain.Dept_inf;
import com.hjl.domain.Job_inf;
import com.hjl.util.JDBCUtils;
import org.springframework.jdbc.core.BeanPropertyRowMapper;
import org.springframework.jdbc.core.JdbcTemplate;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;
import java.util.Set;

/**
 * 职位数据访问层
 *
 * @author Lenovo
 * @version 1.0
 * @date 2021/10/8 13:57
 */
public class JobDaoImpl implements JobDao {
    private JdbcTemplate template = new JdbcTemplate(JDBCUtils.getDataSource());

    /**
     * 查找总页数
     *
     * @param condition 状态
     * @return int
     * @author Lenovo
     * @date 2021/10/8 14:13
     */
    @Override
    public int findTotalCount(Map<String, String[]> condition) {
        String sql = "select count(id) from job_inf where 1=1 ";
        StringBuilder sb = new StringBuilder(sql);
        Set<String> keySet = condition.keySet();
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                sb.append(" and ").append(key).append(" like ? ");
                params.add("%" + value + "%");
            }
        }
        return template.queryForObject(sb.toString(), Integer.class, params.toArray());
    }

    /**
     * 查找当前页的数据集合
     *
     * @param start     起始页
     * @param rows      每页记录数
     * @param condition 状态
     * @return java.util.List<com.hjl.domain.Job_inf>
     * @author Lenovo
     * @date 2021/10/8 14:01
     */
    @Override
    public List<Job_inf> findByPage(int start, int rows, Map<String, String[]> condition) {
        String sql = "select * from job_inf where 1 = 1 ";
        StringBuilder sb = new StringBuilder(sql);
        Set<String> keySet = condition.keySet();
        List<Object> params = new ArrayList<>();
        for (String key : keySet) {
            if ("currentPage".equals(key) || "rows".equals(key)) {
                continue;
            }
            String value = condition.get(key)[0];
            if (value != null && !"".equals(value)) {
                sb.append(" and ").append(key).append(" like ? ");
                params.add("%" + value + "%");
            }
        }
        sb.append(" limit ? , ?");
        params.add(start);
        params.add(rows);
        System.out.println(sb.toString());
        System.out.println(params);
        return template.query(sb.toString(), new BeanPropertyRowMapper<>(Job_inf.class), params.toArray());
    }

    /**
     * 删除
     *
     * @param id ID
     * @author Lenovo
     * @date 2021/10/8 14:13
     */
    @Override
    public void delJob(int id) {
        String sql = "delete from job_inf where ID = ?";
        template.update(sql, id);
    }

    /**
     * 通过id查找（用于编辑）
     *
     * @param id ID
     * @return com.hjl.domain.Job_inf
     * @author Lenovo
     * @date 2021/10/8 14:14
     */
    @Override
    public Job_inf findJobById(int id) {
        String sql = "select * from job_inf where id = ?";
        return template.queryForObject(sql, new BeanPropertyRowMapper<>(Job_inf.class), id);
    }

    /**
     * 更新
     *
     * @param job 职位对象
     * @author Lenovo
     * @date 2021/10/8 14:14
     */
    @Override
    public void updateJob(Job_inf job) {
        String sql = "update job_inf set NAME=?,REMARK=? WHERE ID=?";
        template.update(sql, job.getName(), job.getRemark(), job.getId());
    }

    /**
     * 添加
     *
     * @param job 职位对象
     * @author Lenovo
     * @date 2021/10/8 14:15
     */
    @Override
    public void addJob(Job_inf job) {
        String sql = "insert into  job_inf values(null,?,?)";
        template.update(sql, job.getName(), job.getRemark());
    }
}
